<?php

namespace App\Http\Controllers\System;

use App\Http\Controllers\Controller;

class MenuController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
    }

    /**
     * 列表页面
     */
    public function index()
    {
        $menu_id = request('curr_menu_id', 0);
        if(!$menu_id){
            return response()->json('非法操作');
        }
        $toolBarButton = $this->buildToolBarButton($menu_id);
        return view('system.menu.list',compact('menu_id','toolBarButton'));
    }



    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
     */
    public function listData()
    {
        try{
            $listTree = callApiServer("System.MenuService.listTreeGridData");

            return response()->json(array_values($listTree));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
     */
    public function treeData()
    {
        try{
            $listTree = callApiServer("System.MenuService.listTreeData");

            return response()->json(array_values($listTree));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function showAddForm()
    {
        $parent_id = request('parent_id');
        $parent_name = request('parent_name');
        return view('system.menu.form', compact('parent_id','parent_name'));
    }

    public function showEditForm()
    {
        $params['id'] = request('menu_id');
        try{
            $info = callApiServer("System.MenuService.getInfoById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }
            if($info['parent_id']){
                $parent_info = callApiServer("System.MenuService.getInfoById", ['id'=> $info['parent_id']]);
                $info['parent_name'] = $parent_info['name'];
                $parent_id = $info['parent_id'];
            }else{
                $parent_id=0;
                $info['parent_name'] = 0;
            }
            return view('system.menu.form', compact("info"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function save()
    {
        $params['id'] = request('id');
        $params['code'] = request('code');
        $params['name'] = request('name');
        $params['parent_id'] = request('parent_id');
        $params['icon_style'] = request('icon_style');
        $params['menu_url'] = request('menu_url');
        $params['permission'] = request('permission');
        $params['sort'] = request('sort');
        $params['is_active'] = request('is_active','off') == 'on'?1:0;

        if(empty($params['name'])){
            return response()->json(['code'=>400,'data'=>'标签名称不能为空']);
        }

        $rs = callApiServer("System.MenuService.saveData", $params);
        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function getMenuBtnlist(){
        $params['menu_id'] = request('menu_id');
        $params['page'] = request('page', 1);
        $params['rows'] = request('rows', 999);
        try{
            $list = callApiServer("System.MenuService.getMenuBtn", $params);

            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function setBtn(){
        $menu_id = request('menu_id');

        return view('system.menu.button', compact("menu_id"));

    }

    public function saveBtn(){
        $params['ids'] = request('ids');
        $params['menu_id'] = request('menu_id');

        $rs = callApiServer("System.MenuService.saveBtn", $params);

        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    public function setMenuData(){
        $menu_id = request('menu_id');

        return view('system.menu.data', compact("menu_id"));
    }

    public function getMenuDatalist(){
        $params['menu_id'] = request('menu_id');
        try{
            $list = callApiServer("System.MenuService.getMenuDatalist", $params);

            return response()->json($list);
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>400,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }
    }

    public function addMenuRuleForm(){
        $menu_id = request('menu_id');
        return view('system.menu.ruleform', compact('menu_id'));
    }

    public function saveMenuRule(){
        $params['id'] = request('id');
        $params['name'] = request('name');
        $params['rule'] = request('rule');
        $params['is_active'] = request('is_active')=='on'?'1':0;
        $params['menu_id'] = request('menu_id');

        $rs = callApiServer("System.MenuService.saveMenuRule", $params);

        if($rs){
            return response()->json(['code'=>200]);
        }
        return response()->json(['code'=>400,'data'=>$rs]);
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function editMenuRuleForm(){
        $params['id'] = request('id');
        $menu_id = request('menu_id');

        try{
            $info = callApiServer("System.MenuService.getMenuRuleById", $params);
            if(empty($info)){
                return response()->json('对应数据不存在');
            }

            return view('system.menu.ruleform',compact("info","menu_id"));
        }catch (\Exception $e){
            if($e->getCode() == 500){
                return response()->json(['code'=>500,'data'=>'后台服务异常:'.$e->getMessage()]);
            }else{
                return response()->json(['code'=>400,'data'=>$e->getMessage()]);
            }
        }

    }
}
